package com.zxy.leetcode._00000_00099._00010_00019;

/**
 * https://leetcode-cn.com/problems/integer-to-roman/
 * 整数转罗马数字
 *
 * 给定一个整数，将其转为罗马数字。输入确保在 1 到 3999 的范围内。
 */
public class Test00012 {

    public static void main(String[] args) {
        Test00012 test = new Test00012();
        System.out.println(test.intToRoman(3));
        System.out.println(test.intToRoman(4));
        System.out.println(test.intToRoman(9));
        System.out.println(test.intToRoman(10));
        System.out.println(test.intToRoman(58));
        System.out.println(test.intToRoman(1994));
    }

    public String intToRoman(int num) {
        String[] str4 = {"IV", "XL", "CD"};
        String[] str9 = {"IX", "XC", "CM"};
        String[] str5 = {"V", "L", "D"};
        String[] str10 = {"I", "X", "C", "M"};

        StringBuilder stringBuilder = new StringBuilder();
        int c = 0;
        while (num > 0) {
            int a = num % 10;
            switch (a) {
                case 0:
                    break;
                case 1:
                    stringBuilder.insert(0, str10[c]);
                    break;
                case 2:
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str10[c]);
                    break;
                case 3:
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str10[c]);
                    break;
                case 4:
                    stringBuilder.insert(0, str4[c]);
                    break;
                case 5:
                    stringBuilder.insert(0, str5[c]);
                    break;
                case 6:
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str5[c]);
                    break;
                case 7:
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str5[c]);
                    break;
                case 8:
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str10[c]);
                    stringBuilder.insert(0, str5[c]);
                    break;
                case 9:
                    stringBuilder.insert(0, str9[c]);
                    break;
            }
            c ++;
            num = num / 10;
        }
        return stringBuilder.toString();
    }

    /**
     * 官方的解法
     */
    public String intToRomanFromOfficial(int num) {
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

        StringBuilder sb = new StringBuilder();
        // Loop through each symbol, stopping if num becomes 0.
        for (int i = 0; i < values.length && num >= 0; i++) {
            // Repeat while the current symbol still fits into num.
            while (values[i] <= num) {
                num -= values[i];
                sb.append(symbols[i]);
            }
        }
        return sb.toString();
    }


}
